package com.bluemsun.naturallanguageprocessing.service.impl;

import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.dao.UserMapper;
import com.bluemsun.naturallanguageprocessing.dao.UserTagMapper;
import com.bluemsun.naturallanguageprocessing.entity.UserApi;
import com.bluemsun.naturallanguageprocessing.service.IUserTagService;
import com.bluemsun.naturallanguageprocessing.util.ConductRedisUtil;
import com.bluemsun.naturallanguageprocessing.vo.BrowseVO;
import com.bluemsun.naturallanguageprocessing.vo.TagVO;
import com.bluemsun.naturallanguageprocessing.vo.UserTagVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：hj
 * @date ：Created in 2021/4/25 16:44
 * @description：用户选择标签
 * @modified By：
 * @version: $
 */
@Service("IUserTagService")
public class UserTagServiceImpl implements IUserTagService {

    @Autowired
    UserTagMapper userTagMapper;
    @Autowired
    ConductRedisUtil conductRedisUtil;
    @Autowired
    UserMapper userMapper;

    @Override
    public ServerResponse getTag() {
        List<TagVO> list =userTagMapper.getUserTag();

        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse insertSelect(HttpServletRequest request, List<Integer> list) {
        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        //该用户已经选择过标签
        userMapper.update(userApi.getId());
        Integer id = userApi.getId();
        List<UserTagVO> list1 = new ArrayList<>();
        for(Integer l:list){
            UserTagVO userTagVO =new UserTagVO();
            userTagVO.setTagId(l);
            userTagVO.setUserId(id);
            list1.add(userTagVO);
        }
        userTagMapper.insert(list1);
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse getUserTag(HttpServletRequest request) {
        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        List list = userTagMapper.getUserSelect(userApi.getId());
        if(list ==null ||list.size()==0){
            return ServerResponse.createBySuccess(null);
        }
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse updateUserTag(HttpServletRequest request, List<Integer> list) {
        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        userTagMapper.deleteUserTag(userApi.getId());
        if(list==null||list.size()==0){
            return ServerResponse.createBySuccess("成功清除");
        }
        List<UserTagVO> list1 = new ArrayList<>();
        for(Integer i:list){
            UserTagVO userTagVO =new UserTagVO();
            userTagVO.setUserId(userApi.getId());
            userTagVO.setTagId(i);
            list1.add(userTagVO);
        }
        userMapper.update(userApi.getId());
        userTagMapper.insert(list1);
        return ServerResponse.createBySuccess("成功更改");
    }

    @Override
    public ServerResponse isSelect(HttpServletRequest request) {
        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        //该用户第一次登录
        if(userMapper.isSelect(userApi.getId())==0){
            userMapper.update(userApi.getId());
            return ServerResponse.createBySuccess("该用户未登录过");
        }

        return ServerResponse.createByErrorCodeMessage(2,"该用户已经登录");
    }

}
